<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      input {
        margin-bottom: 20px;
      }
      .tdlist {
        display: flex;
        width: 330px;
        height: 30px;
        align-items: center;
        position: relative;
      }

      .item {
        width: 330px;
        height: 30px;
        border: 1px solid;
        cursor: pointer;
      }
      span {
        position: absolute;
        left: 300px;
        cursor: pointer;
      }
      .active {
        background-color: aqua;
        text-decoration: line-through;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <input
        type="text"
        placeholder="请输入任务"
        v-model="name"
        @keyup.enter="add"
      />
      <button @click="add">添加</button>
      <div v-for="(item,index) in list" class="tdlist">
        <div
          class="item"
          :class="{active:item.checked==true}"
          @click="toggle(item)"
        >
          {{item.name}}
        </div>
        <span @click="remove(index)">x</span>
      </div>
    </div>
    <script>
      const app = new Vue({
        el: "#app",
        data() {
          return {
            name: "",
            isactive: false,
            list: [
              { name: "task1", checked: false },
              { name: "task2", checked: true },
              { name: "task3", checked: false },
            ],
          };
        },
        methods: {
          // 添加数据
          add() {
            if (this.name) {
              this.list.push({
                name: this.name,
                checked: false,
              });
              this.name = "";
            }
          },
          //   切换数据
          toggle(item) {
            item.checked = !item.checked;
          },
          //   删除数据
          remove(index) {
            this.list.splice(index, 1);
          },
        },
      });
    </script>
  </body>
</html>
